﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsDiscoveryEngine;
using NewsMine.DomainObjects;
using NewsMine.Storage;
using System.Net;
using NewsMine.Utilities;

namespace NewsDiscoveryEngine
{
    [Serializable]
    public class PrepareFacebookPostDataFromUserSelectionJob : BaseJob
    {
        public FaceBookEmailPostData FacebookEmailPostInputData { get; set; }

        public int RetryCount { get; set; }

        public override bool ExecuteJob()
        {
            //todo: need to process the job and return the records.

            var result = PrepareRequiredDataForFacebookPostImageGeneration(this.FacebookEmailPostInputData);

            //ProcessOutput(result); //being no work flow for this. Just throwing this into facebook request.

            if (result.FeedItem == null)
            {
                if (this.RetryCount < 20)
                {
                    ++this.RetryCount;
                    Defaults.FaceBookRetryJobQueue.Enqueue(this);
                }
                else
                {
                    //this is only for the testing purpose, here we may need to introduce the proper error (Named application exception) stuff.
                    TempLogger.LogCustom(@"C:\FacebookDataPreparationFailed.txt", "Error FacebookPostDataPreparationFailed.... Facebook post data preparetion failed, due to feedItem not found for the url after 20 retries. Url is : " + this.FacebookEmailPostInputData.FeedId);
                }
            }
            else
            {
                Defaults.FacebookPublishRequestQueue.Enqueue(result);
            }

            return true;
        }

        public static FaceBookEmailPostData PrepareRequiredDataForFacebookPostImageGeneration(FaceBookEmailPostData FacebookEmailPostInputData)
        {
            //here we need to get the feed for that perticular selected url; and simply send it back.

            var feed = (FeedItem)FeedIndexSearcher.Instance.GetFeedById(FacebookEmailPostInputData.FeedId);

            if (feed != null)
            {
                FacebookEmailPostInputData.FeedItem = feed;

                //if (!string.IsNullOrEmpty(FacebookEmailPostInputData.FeedItem.ImageUrl))
                //{
                //    try
                //    {
                //        var webClient = new WebClient();
                //        byte[] imageBytes = webClient.DownloadData(FacebookEmailPostInputData.FeedItem.ImageUrl);

                //        FacebookEmailPostInputData.IsImageDataSuccussfullyDownloaded = true;
                //    }
                //    catch(Exception ex)
                //    {
                //        TempLogger.Log("Error while downloading picture for facebook", ex);
                //        FacebookEmailPostInputData.IsImageDataSuccussfullyDownloaded = false;
                //    }
                //}
            }


            return FacebookEmailPostInputData;
        }
    }
}
